#ifndef FFMPEG_DECODER_H
#define FFMPEG_DECODER_H

#include <pthread.h>
#include <jni.h>
#include <Errors.h>

#include "frame.h"
#include "writer.h"
#include <android/log.h>
#include "global.h"

#define FFMPEG_PLAYER_MAX_QUEUE_SIZE 50

using namespace android;

enum media_player_states_1 {
    MEDIA_PLAYER_STATE_ERROR1        = 0,
    MEDIA_PLAYER_IDLE1               = 1 << 0,
    MEDIA_PLAYER_BEGIN_THREAD1       = 1 << 1,
    MEDIA_PLAYER_INITIALIZED1        = 1 << 2,
    MEDIA_PLAYER_PREPARING1          = 1 << 3,
    MEDIA_PLAYER_PREPARED1           = 1 << 4,
	MEDIA_PLAYER_DECODED1            = 1 << 5,
    MEDIA_PLAYER_STARTED1            = 1 << 6,
    MEDIA_PLAYER_PAUSED1             = 1 << 7,
    MEDIA_PLAYER_STOPPED1            = 1 << 8,
    MEDIA_PLAYER_PLAYBACK_COMPLETE1  = 1 << 9
};

class Decoder
{
public:
	Decoder(JNIEnv* env);
    ~Decoder();
    void        setDataSource( char *url);
    void        setDataUrl( char *url, char *url1, char *url2, char *url3, char *url4, char *url5,int count);
    bool        start();
    bool        stop();
    bool        pause();
	bool        isPlaying();
	bool        isBegin();
	bool        getVideoWidth(int *w);
	bool        getVideoHeight(int *h);
	bool        getDuration(int *msec);
	bool		prepare();
	void			setParamCount(int videoParam);
//    static  sp<IMemory>     decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
//    static  sp<IMemory>     decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
//    static  int             snoop(short *data, int len, int kind);
//            status_t        invoke(const Parcel& request, Parcel *reply);
//            status_t        setMetadataFilter(const Parcel& filter);
//            status_t        getMetadata(bool update_only, bool apply_filter, Parcel *metadata);
	bool        suspend();
	bool        resume();

	VideoFrame* processVideo(AVStream* stream);
	void treatSlap(VideoFrame* frame);
	void processSlap(AVFrame* frame,AVStream* stream);
	void decodeYuv(AVStream* stream);
	bool isBlackPicture(AVFrame* frame);
	void releaseResource();

	bool StartScope(const char* scopeFile);
	bool EndScope();
	bool SlapPicture(const char* slapFile);
	void setStop(int isStop);


private:
	char*                   getUrl(int id);
	bool					connect();
	bool					prepareVideo();
	void 					closeVideo();
	static void*				startPlayer(void* ptr);

//	static void 				decodeVideo(VideoFrame* frame, unsigned char* param, AVFrame* avFrame);
//	static void 				decodeAudio(AudioFrame* frame, AVFrame* avFrame);

	void						decodeMovie(void* ptr);

	static void 				frameTreat(MovieFrame* frame);


	long getCurrTime();
	void avStreamFPSTimeBase(AVStream *st, float defaultTimeBase, float *pFPS, float *pTimeBase);

    char*				  mURL;
	char*				  mURL1;
	char*				  mURL2;
	char*				  mURL3;
	char*				  mURL4;
	char*				  mURL5;

	int urlCount;

	double 						mTime;
	pthread_mutex_t             mLock;
	pthread_t					mPlayerThread;




    //Mutex                       mNotifyLock;
    //Condition                   mSignal;
    AVFormatContext*			mMovieFile;

    int 						mVideoStreamIndex;

	struct 	SwsContext* 		mVideoSwsContext;


	AVPicture					mVideoPicture;


	float						mVideoFPS;


	float						mVideoTimeBase;
	bool                        isUrlNotNull;
	int                         curUrlID;

	AVFrame*					mVideoFrame;



    void*                       mCookie;
    media_player_states_1       mCurrentState;
    int                         mDuration;
    int                         mCurrentPosition;
    int                         mSeekPosition;
    bool                        mPrepareSync;
    int                         mStreamType;
    bool                        mLoop;
    float                       mLeftVolume;
    float                       mRightVolume;
    int                         mVideoWidth;
    int                         mVideoHeight;

    int						 mVideoParamCount;

       jmethodID revokeFrameDataMethod;
       jbyteArray slapRGBDataArray;
       jmethodID revokeSlapDataMethod;

       bool isIFrame;
       bool isFrameIncompleted;

       //gmq add end

	 //¼�����ͼ���
	 bool mScopeOn;
	 bool findKeyFrame;
	 Writer* record;
	 bool mSlapOn;
	 char *mSlapFile;

	 jmethodID revokeMethod;


};

static int check_interrupt(void *ctx);



#endif // FFMPEG_MEDIAPLAYER_H

